home *** CD-ROM | disk | FTP | other *** search
- Path: gecm.com!usenet
- From: Colin Moore <Colin.Moore@gecm.com>
- Newsgroups: comp.lang.c
- Subject: Re: Hiding a password
- Date: 1 Mar 1996 17:49:02 GMT
- Organization: GEC-Marconi
- Message-ID: <4h7dae$cs2@gcsin3.geccs.gecm.com>
- References: <1996Feb29.224936.137160@forest>
- NNTP-Posting-Host: isd62d.rochstr.gmav.gecm.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1 (Windows; I; 16bit)
-
- ebromber@forest.drew.edu wrote:
- >I recently wrote a password program. However, there is one little flaw I
- >want to fix. When the password is entered, it is visible on the screen. I
- >was wondering if anyone knows how to hide the password by printing
- >asterisks instead of the actual character.
- >Thanks in advance.
- >
- >ebromber@drew.edu
- >
-
-
- RE: hiding a password
-
- I dont know if you are using a PC or unix system....
-
- On a Pc....
- you need to create a loop that reads the keyboard one keypress at a
- time without echo (i.e. 'getch()') then check the inputs for carrage
- return ('\n') if it isn't then place the input into a string array and
- print a star on the screen. if the input is a carrage return then quit
- the loop and place an end of string marker on the end of the string array
- ('\0').
-
- char ch = '\0';
- char sting[100];
- int string_position = 0;
-
- while ((ch = getch()) != '\n') {
- putch('*');
- string[string_position++] = ch;
- }
- string[string_position] = '\0';
-
-
- this can easily be adapted from there to include the use of backspace
- ('\b') by checking for it like the carrage return taking the last input
- back out of the string array and removing the last star(print a space
- over it)
-
-
- char ch = '\0';
- char sting[10];
- int string_position = 0;
-
- while ((ch = getch()) != '\n') {
- /* check for backspace */
- if ((ch == '\b') && (string_position > 0)) {
- printf("\b \b); /* remove star */
- string_position--; /* remove last input */
- }
-
- if (string_position > 8) { /* an string overflow check */
- putch('*');
- string[string_position++] = ch;
- }
- }
- string[string_position] = '\0';
-
-
- 'getch()' does tend to stop ctrl+c from working as well..
-
- As for unix (it been a long time since I used it but) :-
- simular to the pc version but using 'getchar()' insted of 'getch()'
- and turn the input mode of the terminal to raw getting the input and then
- back to normal afterwards (check out tty.h file or terminal escape codes)
- or there may already be a function pervided as I said it was a long time
- ago for me...hope this helps in some way..
-
-